//
//  TargetPickerView.m
//  project
//
//  Created by 圆圆陈 on 2018/1/20.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "TargetPickerView.h"

@interface TargetPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)NSString *returnStr;
@property (nonatomic,strong)NSMutableArray *array;
@end

@implementation TargetPickerView
{
    UIView *backView;
    UIPickerView *targetPicker;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        _returnStr = [NSString string];
        _array = [NSMutableArray array];
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT/2, HHHWIDTH, HHHHEIGHT/2)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), FitW(70), FitH(30));
        [cancelBtn setTitleNormal:HHH_Text(@"退出")];
        [cancelBtn setTitleColorNormal:SNCOLORBLACK];
        [cancelBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(HHHWIDTH-FitW(70), FitH(10), FitW(50), FitH(30));
        [sureBtn setTitleNormal:HHH_Text(@"确定")];
        [sureBtn setTitleColorNormal:SNCOLORBLACK];
        [sureBtn addTouchUpTarget:self action:@selector(sureBtnClick)];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:sureBtn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, FitH(50),HHHWIDTH , 1)];
        line.backgroundColor = SNCOLORGRAYLINE;
        [backView addSubview:line];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(FitW(10), line.bottom, HHHWIDTH-FitW(20), FitH(20))];
        label.text = SN_Text(@"运动目标");
        label.font =  [UIFont systemFontOfSize:FitFont(15)];
        label.textAlignment = NSTextAlignmentLeft;
        [backView addSubview:label];
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(FitW(10), label.bottom, HHHWIDTH-FitW(20), FitH(42))];
        label2.text = SN_Text(@"根据世界卫生组织建议，成人需要每天进行中等强度训练40分钟，相当于快走8000步");
        label2.font =  [UIFont systemFontOfSize:FitFont(12)];
        label2.numberOfLines = 0;
        label2.lineBreakMode = NSLineBreakByWordWrapping;
        label2.textAlignment = NSTextAlignmentLeft;
        [backView addSubview:label2];
        
        targetPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, label2.bottom+FitH(10), HHHWIDTH,backView.height-label2.bottom-FitH(10))];
        targetPicker.delegate = self;
        targetPicker.dataSource = self;
        [backView addSubview:targetPicker];
        for (int i = 1000; i <= 30000; i+=1000)
        {
            [_array addObject:[NSString stringWithFormat:@"%d",i]];
        }
        
        
        UILabel *unitLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH/2-40, 30)];
        unitLabel.center = CGPointMake(targetPicker.center.x+FitW(140), targetPicker.center.y);
        unitLabel.text = HHH_Text(@"步");
        unitLabel.textColor = SNCOLORBLACK;
        unitLabel.font = [UIFont systemFontOfSize:20];
        [backView addSubview:unitLabel];
    }
    return self;
}
-(void)setTarget:(NSInteger)target{
    [targetPicker selectRow:(target-1000)/1000 inComponent:0 animated:NO];
    _returnStr = [_array objectAtIndex:(target-1000)/1000];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _array.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return targetPicker.height/3;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel)
    {
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = SNFONT(FitSimpleSize(20, 20, 30, 30, 30));
        pickerLabel.textColor = SNCOLORBLACK;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    //在该代理方法里添加以下两行代码删掉上下的黑线
    [[pickerView.subviews objectAtIndex:1] setHidden:YES];
    [[pickerView.subviews objectAtIndex:2] setHidden:YES];
    return pickerLabel;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_array objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _returnStr = [_array objectAtIndex:row];
}
-(void)cancelBtnClick{
    [self animationedDown];
}
-(void)sureBtnClick{
    if (self.sureButtonBlock) {
        self.sureButtonBlock(_returnStr);
    }
    [self animationedDown];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:backView]) {
        return NO;
    }
    return YES;
}

@end
